package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.AppDataCache;
import com.lkm.market2_0.entity.OrganizationInfo;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;

import java.util.ArrayList;
import java.util.List;



public class PrintHelp {
    public static String print(OrderEntity orderEntity, String allPrice, List<OrderEntity.GoodEntity> goodList, String add_time, SteelyardInfo steelyardInfo, boolean isSalesJoin){
        String orderId = orderEntity.orderNum;

        String printTable="";

        String startTable= "";
        OrganizationInfo organizationInfo = steelyardInfo.orginfo!=null? steelyardInfo.orginfo: AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg());
        String orgName = (organizationInfo == null ? "" : organizationInfo.getName());
        if(orgName==null)orgName="";

        String unit = StringHelp.isEmpty(steelyardInfo.unit)?"kg":steelyardInfo.unit;
        if(orgName.contains("霞山水产品")){
            
            unit = "斤";
        }

        
        boolean isYS = true;

        

        if(orgName.contains("司前中心市场")){
            isYS = false;
        }else if(orgName.contains("远航水产")){
            isYS = false;
        }else if(orgName.contains("古冈市场")){
            isYS = false;
        }




        startTable += holdLine( orgName);
        startTable+= holdLine("档口名："+steelyardInfo.getStallEntity().getStallName() );
        if(isSalesJoin){
            

        }else {
            if(!isYS){
            	startTable += holdLine("卖家：" + steelyardInfo.getStallEntity().getName());
            	startTable += holdLine("电话：" + steelyardInfo.getStallEntity().getPhone());
		    }
        }
        startTable+= holdLine("溯源编码：" +  orderId);
        if(isSalesJoin){
            startTable += holdLine("--------------------------------");
            startTable+= holdLine("进货时间：" + add_time);
        }else if(orderEntity.isSalesReturn()){
            startTable+= holdLine("退货时间：" + add_time);
        }else {
            startTable += holdLine("交易时间：" + add_time);
        }


        String goodTable="";



        String printWeight = printWeight(goodList, unit);
        if(!StringHelp.isEmpty(printWeight)) {
            goodTable += holdLine("--------------------------------");
        }

        goodTable+= printWeight;


        String printNum = printNum(goodList);
        if(!StringHelp.isEmpty(printNum)) {
            goodTable += holdLine("--------------------------------");
        }
        goodTable+= printNum;


        String endTable="";
        endTable += holdLine(("合计?"+allPrice+"元").replaceAll("[?]",getSpaceChars(32-getCharWidth("合计"+allPrice+"元"))));
        

        if(StringHelp.isEmpty(steelyardInfo.codetext)){

            endTable+=holdLineCenter("扫描二维码查看溯源信息");

        }else{
            endTable+=holdLine(steelyardInfo.codetext);
        }



        printTable=startTable+goodTable+endTable;





        return printTable;
    }
    private static String printNum(List<OrderEntity.GoodEntity> goodList_){
        List<OrderEntity.GoodEntity> goodList =new ArrayList<>();
        if(goodList_!=null)
        for (int i = 0; i <goodList_.size(); i++) {
            OrderEntity.GoodEntity good=goodList_.get(i);
            if(good.unitType==2){
                goodList.add(good);
            }
        }
        if(CollectionHelp.isEmpty(goodList)){
            return "";
        }

        String goodTable = "";
        goodTable+= holdLine(" 名称?单价?数量?金额".replaceAll("[?]","     "));

        
        
        goodTable+= holdLine("  ?(元/件)?(件)?(元)".replaceAll("[?]","     "));

        for (int i = 0; i <goodList.size(); i++) {

            OrderEntity.GoodEntity good=goodList.get(i);
            if(good.unitType!=2){
                continue;
            }

            String itemStr="";
            String name= StringHelp.isEmpty(good.goodName)?"商品":good.goodName;
            if(name.length()>4)
                name=name.substring(0,4);
            itemStr+=name+(getSpaceChars(8-getCharWidth(name)));

            String price= SteelardHelp.formatMoney(good.unitPrice );// doubleScaleToString(good.unitPrice ,2, BigDecimal.ROUND_HALF_DOWN);// ""+String .format("%.2f",good.getUnitPrice());;
            itemStr+=(getSpaceChars(7-getCharWidth(price)))+price+" ";

            String weight= NumberHelp.formatMoney(good.weight,2) ;//NumberHelp.doubleScaleToString(good.weight,2,BigDecimal.ROUND_HALF_DOWN);
            itemStr+=(getSpaceChars(7-getCharWidth(weight)))+weight+" ";
//            good.unitPrice*good.weight
            String totalPrice= SteelardHelp.formatMoney(good.calPrice());//NumberHelp.doubleScaleToString(good.unitPrice*good.weight ,2,BigDecimal.ROUND_HALF_DOWN);
            itemStr+=(getSpaceChars(8-getCharWidth(totalPrice)))+totalPrice;

            if(StringHelp.isEmpty(good.goodCode)){

            }else{
                itemStr+=holdLine("编码："+good.goodCode);
            }
            if(StringHelp.isEmpty(good.address)){

            }else{
                itemStr+=holdLine("产地："+good.address);
            }
            if (!StringHelp.isEmpty(good.checkResult)) {
                itemStr += holdLine(good.checkResult);
            }


            goodTable+=holdLine(itemStr);
        }
        return goodTable;
    }
    private static String printWeight(List<OrderEntity.GoodEntity> goodList_,String unit){

        List<OrderEntity.GoodEntity> goodList =new ArrayList<>();
        if(goodList_!=null)
        for (int i = 0; i <goodList_.size(); i++) {
            OrderEntity.GoodEntity good=goodList_.get(i);
            if(good.unitType==1){
                goodList.add(good);
            }
        }
        if(CollectionHelp.isEmpty(goodList)){
            return "";
        }

        String goodTable = "";
        goodTable+= holdLine(" 名称?单价?净重?金额".replaceAll("[?]","     "));






            goodTable+= holdLine(("  ?(元/"+unit+")?("+unit+")?(元)").replaceAll("[?]","     "));

        for (int i = 0; i <goodList.size(); i++) {

            OrderEntity.GoodEntity good=goodList.get(i);
            if(good.unitType!=1){
                continue;
            }

            String itemStr="";
            String name= StringHelp.isEmpty(good.goodName)?"商品":good.goodName;
            if(name.length()>4)
                name=name.substring(0,4);
            itemStr+=name+(getSpaceChars(8-getCharWidth(name)));

            String price= SteelardHelp.formatMoney(good.unitPrice );// doubleScaleToString(good.unitPrice ,2, BigDecimal.ROUND_HALF_DOWN);// ""+String .format("%.2f",good.getUnitPrice());;
            itemStr+=(getSpaceChars(7-getCharWidth(price)))+price+" ";

            String weight=NumberHelp.formatMoney(good.weight,3) ;//NumberHelp.doubleScaleToString(good.weight,2,BigDecimal.ROUND_HALF_DOWN);
            itemStr+=(getSpaceChars(7-getCharWidth(weight)))+weight+" ";

//            good.unitPrice*good.weight
            String totalPrice= SteelardHelp.formatMoney(good.calPrice());//NumberHelp.doubleScaleToString(good.unitPrice*good.weight ,2,BigDecimal.ROUND_HALF_DOWN);
            itemStr+=(getSpaceChars(8-getCharWidth(totalPrice)))+totalPrice;

            if(StringHelp.isEmpty(good.goodCode)){

            }else{
                itemStr+=holdLine("编码："+good.goodCode);
            }

            if(StringHelp.isEmpty(good.address)){

            }else{
                itemStr+=holdLine("产地："+good.address);
            }

            if (!StringHelp.isEmpty(good.checkResult)) {
                itemStr += holdLine(good.checkResult);
            }

            goodTable+=holdLine(itemStr);
        }
        return goodTable;
    }

    private static String holdLine(String s){








        return s + getSpaceChars( (getCharWidth(s)%32)==0? 0 : (32- (getCharWidth(s)%32)) );
    }

    

    private static String holdLineCenter(String s){
        float rl=(32-(getCharWidth(s)%32))/2.0f;
        
        return holdLine(getSpaceChars((int)Math.ceil(rl))+s+getSpaceChars((int)Math.floor(rl)));
    }

    
    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }

    

    public static int getCharWidth(String strName) {
        int charWidth=0;
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                charWidth+=2;
            }else{
                charWidth+=1;
            }
        }
        return charWidth;
    }

    private static String getSpaceChars(int num){
        String sc="";
        for (int i = 0; i < num; i++) {
            sc+=" ";
        }
        return sc;
    }

}
